package com.yunwaikeji.gy_tool_library.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

/**
 * 搜索框
 */
public class GYSearchView extends LinearLayout {

    private LinearLayout hintLL;
    private ImageView hintIV;
    private TextView hintTV;
    private EditText searchET;
    private int imageWidthPx = -1;
    private int imageHeightPx = -1;
    private float hintTextSize = -1;
    private int hintTextColor = -1;
    private float textSize = -1;
    private int textColor = -1;

    public GYSearchView setText(String text) {
        searchET.setText(text);
        return this;
    }

    public GYSearchView setHint(String hint) {
        hintTV.setText(hint);
        return this;
    }

    public GYSearchView setSearchImageResource(int resId) {
        hintIV.setImageResource(resId);
        showHintImage(true);
        return this;
    }

    public GYSearchView showHintImage(boolean isShow) {
        hintIV.setVisibility(isShow ? VISIBLE : GONE);
        return this;
    }

    public GYSearchView(Context context) {
        super(context);
        init();
    }

    public GYSearchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GYSearchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        initView();
    }

    private void initView() {
        RelativeLayout relativeLayout = new RelativeLayout(getContext());
        relativeLayout.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        relativeLayout.setBackground(colorRadiusDrawable(0xf3f3f5, dp2px(getContext(), 4)));
        relativeLayout.setPadding(dp2px(getContext(), 11), 0, dp2px(getContext(), 11), 0);
        relativeLayout.setGravity(Gravity.CENTER_VERTICAL);

        hintLL = new LinearLayout(getContext());
        hintLL.setOrientation(LinearLayout.HORIZONTAL);
        hintLL.setGravity(Gravity.CENTER_VERTICAL);

        hintIV = new ImageView(getContext());
        hintIV.setVisibility(View.GONE);
        hintTV = new TextView(getContext());
        hintLL.addView(hintIV);
        hintLL.addView(hintTV);

        searchET = new EditText(getContext());
        searchET.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        searchET.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        searchET.setBackgroundColor(Color.TRANSPARENT);

        searchET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                hintLL.setVisibility(TextUtils.isEmpty(searchET.getText().toString()) ? View.VISIBLE : View.GONE);
            }
        });

        relativeLayout.addView(hintLL);
        relativeLayout.addView(searchET);

        addView(relativeLayout);

        initHintImage();
        initHintText();
        initText();
    }

    private void initHintImage() {
        if (imageWidthPx == -1) {
            imageWidthPx = dp2px(getContext(), 13);
        }
        if (imageHeightPx == -1) {
            imageHeightPx = dp2px(getContext(), 13);
        }
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, 0, dp2px(getContext(), 10), 0);
        layoutParams.width = imageWidthPx;
        layoutParams.height = imageHeightPx;
        hintIV.setLayoutParams(layoutParams);
    }

    private void initHintText() {
        if (hintTextSize == -1) {
            hintTextSize = sp2px(getContext(), 13);
        }
        if (hintTextColor == -1) {
            hintTextColor = 0xA0A8B6;
        }
        hintTV.setTextSize(hintTextSize);
        hintTV.setTextColor(hintTextColor);
    }

    private void initText() {
        if (textSize == -1) {
            textSize = px2sp(getContext(),13);
        }
        if (textColor == -1) {
            textColor = 0xA0A8B6;
        }
        searchET.setTextSize(textSize);
        searchET.setTextColor(textColor);
    }

    /**
     * 将px值转换为dip或dp值，保证尺寸大小不变
     *
     * @param context
     * @param pxValue （DisplayMetrics类中属性density）
     * @return
     */
    private int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 将dip或dp值转换为px值，保证尺寸大小不变
     *
     * @param context
     * @param dipValue （DisplayMetrics类中属性density）
     * @return
     */
    private int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    /**
     * 将px值转换为sp值，保证文字大小不变
     *
     * @param context
     * @param pxValue （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    private int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     *
     * @param context
     * @param spValue （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    private int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }


    /**
     * 获取颜色图
     *
     * @param color
     * @param radius
     * @return
     */
    private GradientDrawable colorRadiusDrawable(int color, int radius) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setCornerRadius(radius);
        gradientDrawable.setColor(color);
        return gradientDrawable;
    }
}
